import { inject, observer} from 'mobx-react';
import { WeaLeftTree } from 'ecCom';
import * as mobx from 'mobx';

@observer
class ListLeftTree extends React.Component{

    onSearchLabelClick= () => {
      const {searchParams} = this.props.listStore;
      const {setShowSearchAd,clearFormFields,initDatas,doSearch,setSelectedTreeKey} = this.props.listStore;
      setShowSearchAd(false);
      setSelectedTreeKey("");
      clearFormFields();
      initDatas({method:"all"});
      doSearch({
          method: 'all',
          viewcondition: searchParams.defaultTabVal
      });
    }
  
    onSelect = (key, topTabCount, countsType) => {
      const {topTab,topTabTemp,searchParams} = this.props.listStore;
      const {setShowSearchAd,doSearch,setSelectedTreeKey,setConditionByTree,setNowViewType} = this.props.listStore;
        setShowSearchAd(false);
        setSelectedTreeKey(key);
        setConditionByTree();
        let viewc = searchParams.defaultTabVal;
        let viewtypeParams = {}
        if(topTabTemp){
            topTabTemp.map(t=>{
                if (countsType && countsType.name && t.groupid == countsType.name) {
                    viewc = t.viewcondition;
                }
            });
        }else{
            topTab.map(t=>{
                if (countsType && countsType.name && t.groupid == countsType.name) {
                    viewc = t.viewcondition;
                }
            });
        }
        if (countsType.name === "flowNew") {
            viewtypeParams.viewtype = 0;
        } else if (countsType.name === "flowRes") {
            viewtypeParams.viewtype = 1;
        } else {
            viewtypeParams.viewtype = 2;
        }
        setNowViewType(viewtypeParams.viewtype);
        doSearch({
            method: key.indexOf("type_")===0 ? "reqeustbytype" : "reqeustbywfid",
            viewcondition: viewc,
            ...viewtypeParams,
        });
    }
    render(){
        const {leftTree,leftTreeCount,leftTreeCountType,selectedTreeKey} = this.props.listStore;
        let datas = mobx.toJS(leftTree);
        return (
            <WeaLeftTree ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftTree@bbkgfe`}
                datas={datas}
                counts={leftTreeCount}
                countsType={leftTreeCountType}
                selectedKeys={[selectedTreeKey]}
                onSearchLabelClick={this.onSearchLabelClick}
                onSelect={this.onSelect} 
            />
        );
    }
}

export default ListLeftTree;